package conversion.utils.converter;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.opencsv.exceptions.CsvException;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * 这是一个数据转换器类，用于将 JSON XML CSV YAML 数据格式相互转换
 */
@Component
public class AllConverter {

    public String convert(String input,String convertType){
        if (convertType.equalsIgnoreCase("csvtoxml")) {
            return fromCsvToXml(input);
        }else if (convertType.equalsIgnoreCase("csvtojson")){
            return fromCsvToJson(input);
        }else if (convertType.equalsIgnoreCase("csvtoyaml")){
            return fromCsvToYaml(input);
        }else if (convertType.equalsIgnoreCase("jsontoxml")){
            return fromJsonToXml(input);
        }else if (convertType.equalsIgnoreCase("jsontocsv")){
            return fromJsonToCsv(input);
        }else if (convertType.equalsIgnoreCase("jsontoyaml")){
            return fromJsonToYaml(input);
        }else if (convertType.equalsIgnoreCase("xmltocsv")){
            return fromXmlToCsv(input);
        }else if (convertType.equalsIgnoreCase("xmltojson")){
            return fromXmlToJson(input);
        }else if (convertType.equalsIgnoreCase("xmltoyaml")){
            return fromXmlToYaml(input);
        }else if (convertType.equalsIgnoreCase("yamltocsv")){
            return fromYamlToCsv(input);
        }else if (convertType.equalsIgnoreCase("yamltoxml")){
            return fromYamlToXml(input);
        }else if (convertType.equalsIgnoreCase("yamltojson")){
            return fromYamlToJson(input);
        }

        return null;
    }

    public String fromCsvToJson(String csvString){
        if (!CsvProcessor.isFormatValid(csvString)) {
            System.out.println("csv格式不正确");
        }
        try {
            return JsonProcessor.formatJson(CsvProcessor.convertToJSON(csvString));
        } catch (IOException | CsvException e ) {
            e.printStackTrace();
        }
        return null;
    }

    public String fromCsvToXml(String csvString){
        if (!CsvProcessor.isFormatValid(csvString)) {
            System.out.println("csv格式不正确");
        }
        try {

//            return XmlProcessor.Formatter(CsvProcessor.toXML(csvString,","));
            return XmlProcessor.formatXML(XmlProcessor.removeIndentationAndNewlines(CsvProcessor.convertToXML(csvString)));
        } catch ( Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public String fromCsvToYaml(String csvString){
        if (!CsvProcessor.isFormatValid(csvString)) {
            System.out.println("csv格式不正确");
        }
        try {
            return CsvProcessor.convertToYAML(csvString);
        } catch (IOException | CsvException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String fromJsonToCsv(String JsonString){
        if (!JsonProcessor.isFormatValid(JsonString)) {
            System.out.println("json格式不正确");
        }
        try {
            return JsonProcessor.convertToCSV(JsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String fromJsonToXml(String JsonString){
        if (!JsonProcessor.isFormatValid(JsonString)) {
            System.out.println("json格式不正确");
        }

        return XmlProcessor.formatXML(JsonProcessor.convertToXML(JsonString));


    }

    public String fromJsonToYaml(String JsonString){
        if (!JsonProcessor.isFormatValid(JsonString)) {
            System.out.println("json格式不正确");
        }
        try {
            return JsonProcessor.convertToYAML(JsonString);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String fromXmlToJson(String XmlString){
        if (!XmlProcessor.isFormatValid(XmlString)) {
            System.out.println("Xml格式不正确");
        }
        try {
            return XmlProcessor.convertToJSON(XmlString);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String fromXmlToCsv(String XmlString){
        if (!XmlProcessor.isFormatValid(XmlString)) {
            System.out.println("Xml格式不正确");
        }
        try {
            return XmlProcessor.convertToCSV(XmlString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String fromXmlToYaml(String XmlString){
        if (!XmlProcessor.isFormatValid(XmlString)) {
            System.out.println("Xml格式不正确");
        }
        try {
            return XmlProcessor.convertToYAMl(XmlString);
        } catch (ParserConfigurationException | IOException | SAXException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String fromYamlToXml(String YamlString){
        if (!YamlProcessor.isFormatValid(YamlString)) {
            System.out.println("Yaml格式不正确");
        }
        try {
            return XmlProcessor.formatXML(YamlProcessor.convertToXML(YamlString));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String fromYamlToCsv(String YamlString){
        if (!YamlProcessor.isFormatValid(YamlString)) {
            System.out.println("Yaml格式不正确");
        }
        try {
            return YamlProcessor.convertToCSV(YamlString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String fromYamlToJson(String YamlString){
        if (!YamlProcessor.isFormatValid(YamlString)) {
            System.out.println("Yaml格式不正确");
        }
        try {
            return JsonProcessor.formatJson(YamlProcessor.convertToJSON(YamlString));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }



}
